package org.nowcoder.leetcode.BSearch;

import java.util.Arrays;

/**
 * Title  : 74. Search a 2D Matrix
 * Source : https://leetcode.cn/problems/search-a-2d-matrix/description/
 * Author : XrazYang
 * Date   : 2023-10-31
 */

public class LeetCode_74 {
    public boolean searchMatrix(int[][] matrix, int target) {
        //扁平化思想： 函数式编程实现，效率太低
        // return Arrays.stream(matrix)
        //         .flatMapToInt(Arrays::stream)
        //         .peek(System.out::println)
        //         .filter(k -> k == target)
        //         .count() > 0;

        //扁平化思想： 效率高
        int[] nums = new int[matrix.length * matrix[0].length];
        for (int i = 0; i < matrix.length; i++) {
            System.arraycopy(matrix[i], 0, nums, i * matrix[i].length, matrix[i].length);
        }
        return Arrays.binarySearch(nums, target) >= 0;
    }
}
